//
//  MallViewController.m
//  土巴兔
//
//  Created by 千锋 on 15/12/28.
//  Copyright (c) 2015年 IOS1503薛凯. All rights reserved.
//

#import "MallViewController.h"
#import "AFNetworking.h"
#import "Default.h"
@interface MallViewController ()<UIWebViewDelegate>
@property (strong,nonatomic)UIWebView* webview;
@property (strong,nonatomic)UIActivityIndicatorView* activityIndicator;
@property (strong,nonatomic)NSMutableString* urlStr;
@property (weak, nonatomic) IBOutlet UIView *navView;
@property (weak, nonatomic) IBOutlet UILabel *navtitle;

@end

@implementation MallViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden=YES;
    // Do any additional setup after loading the view.
    //    [self getData];
    [self createWebView];
}
#pragma mark- 创建一个WebView
- (void)createWebView{
    _webview=[[UIWebView alloc] initWithFrame:CGRectMake(0, 20,DEVICEWIDTH , DEVICEHEIGHT-20)];
    _webview.scrollView.showsVerticalScrollIndicator=NO;
    [self.view addSubview:_webview];
    _webview.delegate=self;
    http://m.to8to.com/mall/?token=5717321_bf586b7c3f00c492f129353e333930a7&channel=%E9%AD%85%E6%97%8F%E5%BA%94%E7%94%A8%E4%B8%AD%E5%BF%83&appversion=3.6.0&systemversion=22&to8to_token=5717321_bf586b7c3f00c492f129353e333930a7&appostype=1&imei=569eb1056c38e1e8&uid=5717321
    //http://m.to8to.com/mall/?token=
    //    5717321_5575b63d4902b5cabd87e143112f8e1a
    //    &channel=%E9%AD%85%E6%97%8F%E5%BA%94%E7%94%A8%E4%B8%AD%E5%BF%83&appversion=3.6.0&systemversion=22&to8to_token=5717321_5575b63d4902b5cabd87e143112f8e1a&appostype=1&imei=569eb1056c38e1e8&uid=5717321
    _urlStr=[[NSMutableString alloc] initWithString:@"http://m.to8to.com/mall/?token="];
    [_urlStr appendString:[[NSUserDefaults standardUserDefaults] objectForKey:@"to8to_token"]];
    NSString* tail1=@"&channel=%E9%AD%85%E6%97%8F%E5%BA%94%E7%94%A8%E4%B8%AD%E5%BF%83&appversion=3.6.0&systemversion=22&to8to_token=";
    [_urlStr appendString:tail1];
    [_urlStr appendString:[[NSUserDefaults standardUserDefaults] objectForKey:@"to8to_token"]];
    NSString* tail2=@"&appostype=1&imei=569eb1056c38e1e8&uid=";
    [_urlStr appendString:tail2];
    [_urlStr appendString:[[NSUserDefaults standardUserDefaults] objectForKey:@"uid"]];
    
    NSURL* url=[NSURL URLWithString:_urlStr];
    NSURLRequest* request=[[NSURLRequest alloc] initWithURL:url];
    [_webview loadRequest:request];
}
#pragma mark- webview协议
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    if ([request.URL.absoluteString isEqualToString:_urlStr]) {
        self.webview.frame=CGRectMake(0, 20,DEVICEWIDTH , DEVICEHEIGHT-20);
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
        [view setTag:108];
        [view setBackgroundColor:[UIColor blackColor]];
        [view setAlpha:0.5];
        [self.view addSubview:view];
        
        _activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];
        [_activityIndicator setCenter:view.center];
        [_activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
        [view addSubview:_activityIndicator];
        
        [_activityIndicator startAnimating];
        _navView.hidden=YES;
        self.tabBarController.tabBar.hidden=NO;
        return YES;
        
    }else{
        self.navView.hidden=NO;
        self.webview.frame=CGRectMake(0, 45,DEVICEWIDTH , DEVICEHEIGHT-45);
        self.tabBarController.tabBar.hidden=YES;
         return YES;
    }
   
}
- (void)webViewDidStartLoad:(UIWebView *)webView{
    
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    [_activityIndicator stopAnimating];
    UIView *view = (UIView*)[self.view viewWithTag:108];
    [view removeFromSuperview];
    
    NSString *title = [_webview stringByEvaluatingJavaScriptFromString:@"document.title"];
    [_navtitle setText:title];
}

- (IBAction)webBack:(UIButton *)sender {
    [_webview goBack];
}
@end
